<?php
namespace app\gr\controller;

use cmf\controller\AdminBaseController;
use app\gr\BLL\CardPubBLL;


class AdminCardPubController extends AdminBaseController
{
    private $bll;
    private $ControlName = "AdminCardPub";

    public function __construct(CardPubBLL $cardPub)
    {
        parent::__construct();
        $this->bll = $cardPub;
    }

    public function index()
    {
        $params = $this->request->param();

        $where= [];
        $request = input('request.');

        if (!empty($request['name'])) {
            $name = $request['name'];

            $hasWhere['name'] = ['like', "%$name%"];
            $res=$this->bll->hasWhere('cardType',$hasWhere);
        }

        if (!empty($request['card_no'])) {
            $cardNo = $request['card_no'];

            $where['card_no'] = ['like', "%$cardNo%"];
        }

        $list=$this->bll->with('cardType')->findWherePage($where,'gr_card_pub.*');
        $list->appends($params);
        // 获取分页显示
        $page = $list->render();

        $this->assign('list', $list);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch();
    }

    public function addPost()
    {
        $filePath = $this->request->param('file_path');
        $pathArray = explode(".", $filePath);
        $filePath = './upload/'.$filePath;

        Vendor("phpoffice.phpexcel.Classes.PHPExcel.IOFactory");
        // Vendor("phpoffice.phpexcel.Classes.PHPExcel.Shared.Date");

        if ($pathArray[1] == 'xls') {
            $objReader = \PHPExcel_IOFactory::createReader('Excel5');
        } else {
            $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
        }
        // $shared = new \PHPExcel_Shared_Date();

        try {
            $objPHPExcel = $objReader->load($filePath);
        } catch (Exception $e) {
        }

        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow();

        $dataList = array();
        $now = time();
        for ($i=2;$i<=$highestRow;$i++) {
            $id = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
            $no = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
            $psd = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
            if(empty($id)||empty($no)||empty($psd)){
                $this->error("不允许存在空资料");
            }
            $dataList[] =array(
                            'create_time'=>$now,
                            'update_time'=>$now,
                            'card_id'=>$id,
                            'card_no'=>$no,
                            'card_psd'=>$psd,
                        );
        }
        if($this->bll->saveall($dataList)){
            $this->success('新增成功', url("$this->ControlName/index"));
        }else{
            $this->error("添加失败！");
        }

        echo $filePath;
    }

    public function delete()
    {
        $id = $this->request->param('id', 0, 'intval');
        if (!empty($id)) {
            if($this->bll->delete($id)){
                $this->success("删除成功！", url("$this->ControlName/index"));
            }else{
                $this->error('删除失败！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }
}
